package com.ptswitch.material.model.material;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.validator.constraints.NotBlank;
import org.hxzon.project.BaseEntity;

@Entity
//@Indexed
public class Bom extends BaseEntity {

    private static final long serialVersionUID = 6705569910023553421L;

    @OneToMany(mappedBy = "bom", fetch = FetchType.LAZY)
    @OrderBy(value = "code")
    @Cascade(value = { org.hibernate.annotations.CascadeType.DELETE })
    private List<BomMaterial> bomMaterials;

    @OneToOne(fetch = FetchType.EAGER)
    @NotFound(action = NotFoundAction.IGNORE)
    @Cascade(value = { org.hibernate.annotations.CascadeType.DELETE })
    private BomOrigFile origFile;

    @Column(columnDefinition = DefineVarchar32)
    @NotNull
    @NotBlank
    @Size(max = DefineSize32)
//	@Field(name="search")
    private String code;

    @Column(columnDefinition = DefineVarchar32)
    @NotNull
    @NotBlank
    @Size(max = DefineSize32)
//	@Field(name="search")
    private String version;

    @Column(columnDefinition = DefineVarchar32)
    @Size(max = DefineSize32)
//	@Field(name="search")
    private String name;

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
    private String fileName;

    @Column(columnDefinition = DefineVarchar2048)
    @Size(max = DefineSize2048)
//	@DataType(TapestryEx.DataType_html)
//	@Field(name="search")
    private String remark;

    // @PrePersist
    // @PreUpdate
    // public void escape(){
    // OgnlUtils.escapeStringProperty(this);
    // }

    public List<BomMaterial> getBomMaterials() {
        return bomMaterials;
    }

    public void setBomMaterials(List<BomMaterial> bomMaterials) {
        this.bomMaterials = bomMaterials;
    }

    public BomOrigFile getOrigFile() {
        return origFile;
    }

    public void setOrigFile(BomOrigFile origFile) {
        this.origFile = origFile;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String toString() {
        return this.code + "::" + this.version;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof Bom)) {
            return false;
        }
        Bom other = (Bom) o;
        return new EqualsBuilder().append(this.code, other.getCode()).append(this.version, other.getVersion()).isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder().append(this.code).append(this.version).toHashCode();
    }

}
